<?php

namespace App\Http\Controllers;

use Laravel\Lumen\Routing\Controller as BaseController;
use App\Http\Service\UserService;
use App\Http\Service\CaptchaService;
use App\Http\Service\TokenService;
use App\Http\Service\ShareLogService;
use App\Http\Service\PointLogService;
use Illuminate\Http\Request;
use App\Http\Model\User;

class UserController extends BaseController
{

    protected $request;
    protected $user;
    protected $userService;
    protected $captchaService;
    protected $tokenService;
    protected $shareLogService;
    protected $pointLogService;

    public function __construct(Request $request,
                                UserService $userService,
                                CaptchaService $captchaService,
                                TokenService $tokenService,
                                ShareLogService $shareLogService,
                                PointLogService $pointLogService)
    {
        $this->request = $request;
        $this->user = $this->request->get('user');
        $this->userService = $userService;
        $this->captchaService = $captchaService;
        $this->tokenService = $tokenService;
        $this->shareLogService = $shareLogService;
        $this->pointLogService = $pointLogService;
    }

    public function login()
    {
        $mobile = $this->request->input('mobile', '');
        $captcha = $this->request->input('captcha', '');
        $token = $this->request->header('token');

        // 手机号验证
        if (!checkMobile($mobile))
            return toJSON('', \Constant::MOBILE_ERROR);

        if (empty($captcha))
            return toJSON('', \Constant::MOBILE_CAPTCHA_ERROR);


        // 手机验证码验证
        $checkCaptcha = $this->captchaService->checkMobileCaptcha($captcha, $this->user, $token, $mobile);
        if (!$checkCaptcha)
            return toJSON('', \Constant::MOBILE_CAPTCHA_ERROR);

        $user = $this->userService->findFirstRowByFields(['mobile' => $mobile]);

        if ($user == null) {
            $user = $this->userService->register($mobile);
            $jwt = encodeToken($user);

            $user->token = $jwt;
            $user->save();

            $token = $this->tokenService->findFirstRowByFields(['token_crc32' => crc32($token), 'token' => $token]);
            $token->user_id = $user->id;
            $token->token = $jwt;
            $token->token_crc32 = crc32($jwt);
            $token->save();
        }

        if ($user == null)
            return toJSON('', \Constant::FAILED);

        return toJSON($user);
    }

    public function avatar()
    {
        $avatar = $this->request->input('avatar', '');

        $this->user->avatar = $avatar;
        $this->user->save();

        return toJSON($this->user);
    }

    public function share()
    {
        $type = $this->request->input('type', 1);

        if (!in_array($type, [1])) {
            return toJSON('', \Constant::PARAM_ERROR);
        }

        if ($type == 1) {
            $shareLog = $this->shareLogService->findFirstRowByFields(['user_id' => $this->user->id, 'type' => $type]);

            if ($shareLog != null) {
                return toJSON('', \Constant::FAILED, '每日朋友圈分享只能获取积分一次');
            }

            $this->shareLogService->create([
                'user_id' => $this->user->id,
                'type' => $type,
            ]);

            $point = 1;
            $this->user->history_points += $point;
            $this->user->points += $point;
            $this->user->save();
            //积分日志
            $this->pointLogService->create([
                'user_id' => $this->user->id,
                'number' => $point,
                'change_desc' => '每日朋友圈分享',
                'change_type' => 4,
            ]);

        }

        return toJSON($this->user);
    }

    public function logout()
    {
        $user = new User;
        $user->id = 0;
        $user->mobile = '';
        $user->nickname = md5(microtime() . rand(0, 5000));

        $jwt = encodeToken($user);
        $token = $this->tokenService->create(['token' => $jwt, 'token_crc32' => crc32($jwt)]);
        $user->token = $token->token;

        return toJSON($user);
    }
}
